/**
 * Created by Administrator on 15-9-25.
 */

//    var id=$.getUrlParam("id");
//   location.href='tongyou.html?id='+id;}
//
window.onload=function() {
    var id = getUserId();
    var url = getUrl();
    $.jsonp({
        url: 'http://'+url+'/api/sameway/sameway_list',
        data: {userId:id},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $.each(data.spotsArray,function(index,ele){
                //console.log(ele.photoName)
                $(".targetSpots").append("<option value='"+ele.spotsId+"'>"+ele.photoName+"</option>")
            })
        },
        error: function (xOptions, textStatus) {

        }
    });
    $.jsonp({
        url: 'http://'+url+'/api/sameway/sameway_list',
        data: {userId:id},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $.each(data.rentArray,function(index,ele){
                //console.log(ele.name)
                $(".rentType").append("<option value='"+ele.id+"'>"+ele.name+"</option>")
            })
        },
        error: function (xOptions, textStatus) {

        }
    });
    $.jsonp({
        url: 'http://'+url+'/api/sameway/sameway_list',
        data: {userId:id},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $.each(data.brightArray,function(index,ele){
                //console.log(ele.name)
                $(".brightSpot").append("<option value='"+ele.id+"'>"+ele.name+"</option>")
            })
        },
        error: function (xOptions, textStatus) {

        }
    });


    $(".log-fb").on("click", function () {
       // var user_id= getUserId();


        //var stagetPicsrtTimeString=$("#startTime").val();
        //var stime=Math.round(new Date(startTimeString).getTime()/1000)
        //getTime(stime)

        //var etime=Math.round(new Date(endTimeString).getTime()/1000)
        //getTime(etime)
        var startTimeString =$("#startTime").val();
        var endTimeString =$("#endTime").val();
        var wayType=$("#wayType").val();
        var goAddress=$("#goAddress").val();
        var targetSpots=$(".targetSpots").val();
        var rentType=$(".rentType").val();
        var wayNumber =$("#wayNumber").val();
        var contact=$("#contact").val();
        var brightSpot=$(".brightSpot").val();
        var wayDesc=$("#wayDesc").val();
        var urlList = getPics();
        if(startTimeString=="")
        {
            showToast("开始时间不能为空~");
            return;
        }
        if(endTimeString=="")
        {
            showToast("结束时间不能为空~");
            return;
        }
        if(startTimeString>endTimeString){
            showToast("开始时间不能大于结束时间~");
            return;
        }
        if(wayType=="")
        {
            showToast("同游类型不能为空~");
            return;
        }
        if(goAddress=="")
        {
            showToast("出发地点不能为空~");
            return;
        }
        if(targetSpots=="")
        {
            showToast("目标景点不能为空~");
            return;
        }
        if(rentType=="")
        {
            showToast("出行方式不能为空~");
            return;
        }
        if(contact=="")
        {
            showToast("电话号码不能为空~");
            return ;
        }
        if(contact.length!=11)
        {
            showToast("手机号格式不正确~");
            return;
        }
        if(brightSpot=="")
        {
            showToast("亮点不能为空~");
            return;
        }
        if(wayDesc=="")
        {
            showToast("同游详情不能为空~");
            return;
        }
        if(urlList=="")
        {
            showToast("请至少上传一张图片~");
            return;
        }

        //console.log({startTimeString:startTimeString,endTimeString:endTimeString,wayType:wayType,goAddress:goAddress,targetSpots:targetSpots,rentType:rentType,wayNumber:wayNumber,contact:contact,brightSpot:brightSpot,wayDesc:wayDesc,urlList:urlList,userId:id})
        $.jsonp({
            url: 'http://'+url+'/api/sameway/dosave',
            data: {startTimeString:startTimeString,endTimeString:endTimeString
            ,wayType:wayType,goAddress:goAddress,targetSpots:targetSpots
            ,rentType:rentType,wayNumber:wayNumber,contact:contact
            ,brightSpot:brightSpot,wayDesc:wayDesc,urlList:urlList,userId:id},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                //alert("成功===");
                showToast("发布成功~");
                location.href='tongyou.html';
                //console.log({startTimeString:startTimeString,endTimeString:endTimeString,wayType:wayType,goAddress:goAddress,targetSpots:targetSpots,rentType:rentType,wayNumber:wayNumber,contact:contact,brightSpot:brightSpot,wayDesc:wayDesc,urlList:urlList,userId:id})
            },
            error: function (xOptions, textStatus) {
                showToast("发布失败~");
            }
        });

    });

    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset : 'datetime'};
    opt.datetime = {preset : 'datetime'};
    opt.time = {preset : 'datetime'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 50, //开始年份
        endYear: currYear + 10 //结束年份
    };

    $("#startTime").mobiscroll($.extend(opt['date'], opt['default']));
    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset : 'datetime'};
    opt.datetime = {preset : 'datetime'};
    opt.time = {preset : 'datetime'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 50, //开始年份
        endYear: currYear + 10 //结束年份
    };

    $("#endTime").mobiscroll($.extend(opt['date'], opt['default']));
    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset : 'datetime'};
    opt.datetime = {preset : 'datetime'};
    opt.time = {preset : 'datetime'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 50, //开始年份
        endYear: currYear + 10 //结束年份
    };

}
//function getPics(){
//    var imgs=$("#ul img");
//    var paths=[];
//    $.each(imgs,function(index,ele){
//        if(index != 0 && index != imgs.length - 1){
//           paths.push($(ele).attr('src'));
//        }
//
//    })
//    var paths=paths.join(",");
//
//    return paths;
//}
function onBackKeyDown() {
    jumpbyback(1,"");
}
function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now) {
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    return year + "-" + (month >= 10 ? month : "0" + month) + "-" + (date >= 10 ? date : "0" + date);
}